<?php

namespace App\Http\Controllers\Report;

use App\Http\Conf\ResultInfo;
use App\Http\Controllers\Controller;
use App\Http\Models\Business\ExternalOrderDetails;
use App\Http\Models\Business\InvoiceDetails;
use App\Http\Models\Business\Payer;
use App\Http\Models\Report\ReportPayerZ1Scale;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PayerZ1TargetProgressController extends Controller
{
    private $select_where = [
        'year' => 0,
        'payer_code' => 0,
    ];

    private $request_params = [
        'year' => '',
        'payer_code' => '',
    ];


    public function index(Request $request)
    {
        $this->dealRequestParams($request);
        $report_payer_z1_scale = new ReportPayerZ1Scale($this->select_where['year'], $this->select_where['payer_code']);

        return view('report.payer_target_progress.index', [
            'report_data' => $report_payer_z1_scale,
            'request_params' => $this->request_params,
        ]);
    }


    //缓存数据
    public function cache(Request $request)
    {
        $year = $request->year;
        $payer_array = [];
        if ($request->payer_code != '') {
            $payer = Payer::where(['code' => $request->payer_code])->first();
            if ($payer == null) {
                return ['status' => ResultInfo::ERROR, 'info' => '此客户不存在或者没有维护客户基础数据！'];
            }
            $payer_array[$payer->code] = $payer;
        } else {
            $payers = Payer::all();
            foreach ($payers as $payer) {
                $payer_array[$payer->code] = $payer;
            }
        }

        if (count($payer_array) == 0) {
            return ['status' => ResultInfo::ERROR, 'info' => '客户不存在！'];
        }

        foreach ($payer_array as $payer) {
            $payer->year = $year;

            //开票数据 所有
            $invoice_infos = InvoiceDetails::select([
                'category_contract_id',
                DB::raw('sum(total_price_no_tax) as amount'),
                DB::raw('sum(quantity) as quantity'),
            ])
                ->where([
                    'year' => $year,
                    'payer_code' => $payer->code,
                    'distribution_channel_code' => 'Z1',
                ])
                ->where('sales_order_type_code', '<>', 'ZOR5')
                ->groupBy('category_contract_id')
                ->get()->toArray();

            if (count($invoice_infos) > 0) {

                $payer->setPayerZ1ScaleResultForSave($invoice_infos);
            }

            //外部订单
            $external_order_infos = ExternalOrderDetails::select([
                'category_contract_id',
                DB::raw('sum(amount) as amount'),
                DB::raw('sum(quantity) as quantity'),

            ])
                ->where([
                    'year' => $year,
                    'payer_code' => $payer->code
                ])
                ->groupBy('category_contract_id')
                ->get()->toArray();

            if (count($invoice_infos) > 0) {
                $payer->setPayerZ1ScaleResultForSave($external_order_infos);
            }

            //开票数据  正常价
            $invoice_infos = InvoiceDetails::select([
                'category_contract_id',
                DB::raw('sum(total_price_no_tax) as normal_amount'),
            ])
                ->where([
                    'year' => $year,
                    'payer_code' => $payer->code,
                    'distribution_channel_code' => 'Z1',
                    'is_special' => 0
                ])
                ->where('sales_order_type_code', '<>', 'ZOR5')
                ->groupBy('category_contract_id')
                ->get()->toArray();

            if (count($invoice_infos) > 0) {
                $payer->setPayerZ1ScaleResultForSave($invoice_infos);
            }

            //保存客户规模结果数据
            $payer->saveCachePayerZ1ScaleData();
        }

        return ['status' => ResultInfo::SUCCESS];
    }

    /**
     * 处理请求参数
     */
    private function dealRequestParams(Request $request)
    {
        if (is_numeric($request->year)) {
            $this->select_where['year'] = $request->year;
            $this->request_params['year'] = $request->year;
        } else {
            $this->select_where['year'] = date('Y');
            $this->request_params['year'] = date('Y');
        }

        if (is_numeric($request->payer_code)) {
            $this->select_where['payer_code'] = $request->payer_code;
            $this->request_params['payer_code'] = $request->payer_code;
        }
    }
}
